import gameAudio.Audio;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;

/**
 * @author Logical Solutions
 *
 */
public class CutScenes extends JWindow {
	
	private static final File[] intro = {	new File("src/Intro/Intro-001.jpg"),
											new File("src/Intro/Intro-002.jpg"),
											new File("src/Intro/Intro-003.jpg"),
											new File("src/Intro/Intro-004.jpg"),
											new File("src/Intro/Intro-005.jpg"),
											new File("src/Intro/Intro-006.jpg"),
											new File("src/Intro/Intro-007.jpg"),
											new File("src/Intro/Intro-008.jpg"),
											new File("src/Intro/Intro-009.jpg"),
											new File("src/Intro/Intro-010.jpg"),
											new File("src/Intro/Intro-011.jpg"),
											new File("src/Intro/Intro-012.jpg"),
											new File("src/Intro/Intro-013.jpg"),
											new File("src/Intro/Intro-014.jpg"),
											new File("src/Intro/Intro-015.jpg"),
											new File("src/Intro/Intro-016.jpg")};
	
	
	private static BufferedImage bi = new BufferedImage(640,455, BufferedImage.TYPE_INT_ARGB);
	private static final ImageIcon introImage = new ImageIcon(bi);
	
	/*private static final ImageIcon[] end1 = {	new ImageIcon("src/Ending1/Ending1-001.jpg"),
												new ImageIcon("src/Ending1/Ending1-002.jpg"),
												new ImageIcon("src/Ending1/Ending1-003.jpg"),
												new ImageIcon("src/Ending1/Ending1-004.jpg"),
												new ImageIcon("src/Ending1/Ending1-005.jpg"),
												new ImageIcon("src/Ending1/Ending1-006.jpg"),
												new ImageIcon("src/Ending1/Ending1-007.jpg"),
												new ImageIcon("src/Ending1/Ending1-008.jpg"),
												new ImageIcon("src/Ending1/Ending1-009.jpg"),
												new ImageIcon("src/Ending1/Ending1-010.jpg"),
												new ImageIcon("src/Ending1/Ending1-011.jpg"),
												new ImageIcon("src/Ending1/Ending1-012.jpg"),
												new ImageIcon("src/Ending1/Ending1-013.jpg"),
												new ImageIcon("src/Ending1/Ending1-014.jpg"),
												new ImageIcon("src/Ending1/Ending1-015.jpg"),
												new ImageIcon("src/Ending1/Ending1-016.jpg"),
												new ImageIcon("src/Ending1/Ending1-017.jpg"),
												new ImageIcon("src/Ending1/Ending1-018.jpg"),
												new ImageIcon("src/Ending1/Ending1-019.jpg"),
												new ImageIcon("src/Ending1/Ending1-020.jpg"),
												new ImageIcon("src/Ending1/Ending1-021.jpg"),
												new ImageIcon("src/Ending1/Ending1-022.jpg"),
												new ImageIcon("src/Ending1/Ending1-023.jpg"),
												new ImageIcon("src/Ending1/Ending1-024.jpg"),
												new ImageIcon("src/Ending1/Ending1-025.jpg"),
												new ImageIcon("src/Ending1/Ending1-026.jpg"),
												new ImageIcon("src/Ending1/Ending1-027.jpg"),
												new ImageIcon("src/Ending1/Ending1-028.jpg"),
												new ImageIcon("src/Ending1/Ending1-029.jpg"),
												new ImageIcon("src/Ending1/Ending1-030.jpg"),
												new ImageIcon("src/Ending1/Ending1-031.jpg"),
												new ImageIcon("src/Ending1/Ending1-032.jpg"),
												new ImageIcon("src/Ending1/Ending1-033.jpg"),
												new ImageIcon("src/Ending1/Ending1-034.jpg"),
												new ImageIcon("src/Ending1/Ending1-035.jpg"),
												new ImageIcon("src/Ending1/Ending1-036.jpg"),
												new ImageIcon("src/Ending1/Ending1-037.jpg"),
												new ImageIcon("src/Ending1/Ending1-038.jpg"),
												new ImageIcon("src/Ending1/Ending1-039.jpg"),
												new ImageIcon("src/Ending1/Ending1-040.jpg"),
												new ImageIcon("src/Ending1/Ending1-041.jpg"),
												new ImageIcon("src/Ending1/Ending1-042.jpg"),
												new ImageIcon("src/Ending1/Ending1-043.jpg"),
												new ImageIcon("src/Ending1/Ending1-044.jpg"),
												new ImageIcon("src/Ending1/Ending1-045.jpg"),
												new ImageIcon("src/Ending1/Ending1-046.jpg"),
												new ImageIcon("src/Ending1/Ending1-047.jpg"),
												new ImageIcon("src/Ending1/Ending1-048.jpg"),
												new ImageIcon("src/Ending1/Ending1-049.jpg"),
												new ImageIcon("src/Ending1/Ending1-050.jpg")};*/

	JLabel l;
	Thread animate;
	Audio aud = new Audio();
	File file = new File("src/Music/Town Music.mid");
	File file1 = new File("src/Music/fountain.mid");
	File file2 = new File("src/Music/sar2.mid");
	File file3 = new File("src/Music/prelude4.mid");
	
	/*private static final String[] introMusic = {	new String("src/Music/Town Music.mid"),
													new String("src/Music/fountain.mid"),
													new String("src/Music/sar2.mid"),
													new String("src/Music/prelude4.mid")};*/
	
	int cutscene = 1;
	
	public CutScenes(final JFrame f)
	{
		super(f);
		System.out.println(intro.length);
		//if(cutscene==1){
			l = new JLabel(introImage);//}
		/*if(cutscene==2){
			l = new JLabel(end1[0]);}*/
        getContentPane().add(l, BorderLayout.CENTER);
        pack();
        Dimension screenSize =  Toolkit.getDefaultToolkit().getScreenSize();
        Dimension labelSize = l.getPreferredSize();
        setLocation(screenSize.width/2 - (labelSize.width/2),screenSize.height/2 - (labelSize.height/2));
       
        /*addMouseListener(new MouseAdapter()//if mouse is pressed, splash screen disappears
        {
        	public void mousePressed(MouseEvent e)
        	{
        		setVisible(false);
        		dispose();
        		      		
        	}
        });*/
        final int pause = 54000;
        final Runnable closerRunner = new Runnable()//closeRunner-actual game will get loaded as the 
        {											//splash screen is ready to close
        	public void run()
        	{
        		setVisible(false);
        		dispose();
        		startCS();
        		stopCS();
        		setVisible(false);
        		dispose();
        		System.exit(0);
        		//ActiveScreen as = new ActiveScreen(f);
        		//as.startGame();
        	}
        };
        Runnable waitRunner = new Runnable()
        {
        	public void run()
        	{
        		try
        		{
        			Thread.sleep(pause);
                    SwingUtilities.invokeAndWait(closerRunner);
        		}
        		catch(Exception e)
        		{
        			e.printStackTrace();
                    // can catch InvocationTargetException
                    // can catch InterruptedException
                }
             }
        };
        setVisible(true);
        Thread csThread = new Thread(waitRunner, "CSThread");
        csThread.start();
	}
	public void startCS() {

		animate = new Animate();
		animate.start();
		
	}
	
	private void stopCS() {
		animate.stop();
		aud.stopMusic();
	}
	
	public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(bi, 0, 0, this);
    }
	class Animate extends Thread {
		public void run()
		{
			try {
				//while(true)
				//{	
					if(cutscene==1){
						for(int i=0; i < intro.length; i++){
							if(i==0){
								aud.playMusic(file,50);
							}
							if(i==8){
								aud.playMusic(file1, 51);
							}
							display(i,3000);
							/*display(0,1000);
							display(1,1000);
							//aud.playMusic(file, 51);
							display(2,1000);
							display(3,3000);
							display(4,3000);
							display(5,3000);
							display(6,2000);
							display(7,2000);
							aud.playMusic(file1, 52);
							display(8,1000);
							display(9,1500);
							display(10,1500);
							display(11,1500);
							display(12,500);
							display(13,500);
							display(14,500);
							display(15,500);
							display(16,2500);*/
						}
						//cutscene = 0;
					}
					/*if(cutscene==2){
						for(int i=0; i < end1.length; i++){
							if(i==0){
								aud.playMusic(file2,150);
							}
							if(i==32){
								aud.playMusic(file3,151);
							}
							display(i,1000);
							System.out.println("endi: " + i);
						}
						cutscene = 0;
					}*/
					cutscene = 2;
				//}
			}
			catch (Exception e) {
				if(e instanceof InterruptedException)
				{}
			}
		}
		void display(int image,int delay) throws InterruptedException
		{
			if(cutscene==1){
				try {
		            BufferedImage img = ImageIO.read(intro[image]);		           
		            Graphics g = bi.getGraphics();
		            g.drawImage(img, 0, 0, null);
		            g.dispose();

		        } catch (IOException e) {
		            System.out.println("Image could not be read");
		            System.exit(1);
		        }
		        
				//introImage.setImage(arg0)
				//l.setIcon(introImage);
				//l.setIcon(intro[image]);
			}
			if(cutscene==2){
				//l.setIcon(end1[image]);
			}
			l.repaint();
			if(Thread.interrupted()) throw(new InterruptedException());
			Thread.sleep(delay);
		}
	}
}
